package com.das.metrocloud.ssoserver.service.impl;

import com.das.metrocloud.ssoserver.service.*;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import com.das.framework.common.extvo.*;
import com.das.framework.common.redis.utils.*;
import com.das.framework.common.utils.*;

@Service
public class AuthorizationServiceImpl implements AuthorizationService
{
    @Autowired
    private RedisUtils redisUtils;
    
    @Override
    public ResultVO authorization(final String token) {
        return this.validatorToken(token);
    }
    
    @Override
    public ResultVO hasPermission(final String token, final String codes) {
        final ResultVO resultVO = this.validatorToken(token);
        if (!resultVO.isSuccess()) {
            return resultVO;
        }
        final TokenVo tokenVo = (TokenVo)resultVO.getData();
        final String[] split;
        final String[] arr = split = codes.split(";");
        for (final String code : split) {
            if (!this.redisUtils.hHasKey(RedisCacheKeyUtils.userPermissionKey(tokenVo.getUserId()), code)) {
                resultVO.setSuccess(false);
                return resultVO;
            }
        }
        resultVO.setSuccess(true);
        return resultVO;
    }
    
    private ResultVO validatorToken(final String token) {
        final ResultVO resultVO = new ResultVO();
        if (Tools.StringIsNullOrSpace(token)) {
            resultVO.setSuccess(false);
            resultVO.setMessage("\u8bf7\u6c42\u4fe1\u606f\u4e0d\u5b8c\u6574.");
            return resultVO;
        }
        final TokenVo tokenVo = (TokenVo)this.redisUtils.get(token.trim());
        if (tokenVo == null) {
            resultVO.setSuccess(false, "401");
            resultVO.setMessage("\u672a\u8ba4\u8bc1\u6216\u5df2\u8fc7\u671f.");
            return resultVO;
        }
        if (tokenVo.getStatus() != 0) {
            resultVO.setSuccess(false, "403");
            resultVO.setMessage("\u8ba4\u8bc1\u5df2\u88ab\u7981\u7528.");
            return resultVO;
        }
        resultVO.setSuccess(true, "200");
        resultVO.setMessage("\u901a\u8fc7\u8ba4\u8bc1");
        resultVO.setData((Object)tokenVo);
        return resultVO;
    }
}
